package com.mes.loco.aps.server.controller.oms;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.APSService;
import com.iplant.aps.service.CoreService;
import com.iplant.aps.service.OMSService;
import com.iplant.aps.service.mesenum.OMSOrderPriority;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.aps.APSCondition;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.excel.ExcelData;
import com.iplant.aps.service.po.oms.OMSCheckMsg;
import com.iplant.aps.service.po.oms.OMSOrder;
import com.iplant.aps.service.po.oms.OMSOrderLessQuestionItem;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.dao.BaseDAO;
import com.iplant.aps.utils.RetCode;
import com.iplant.aps.utils.aps.ExcelReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author PengYouWang
 * 2019年12月26日22:46:00
 */
@RestController
@RequestMapping("/api/OMSOrder")
public class OMSOrderController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(OMSOrderController.class);

    final
    OMSService wOMSService;

    final
    APSService wAPSService;

    final
    CoreService wCoreService;

    public OMSOrderController(OMSService wOMSService, APSService wAPSService, CoreService wCoreService) {
        this.wOMSService = wOMSService;
        this.wAPSService = wAPSService;
        this.wCoreService = wCoreService;
    }

    /**
     * 查单条
     */
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            int wID = StringUtils.parseInt(request.getParameter("ID"));

            BMSEmployee wLoginUser = this.GetSession(request);

            ServiceResult<OMSOrder> wServiceResult = wOMSService.OMS_QueryOMSOrder(wLoginUser, wID);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 条件查询
     */
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            int wID = StringUtils.parseInt(request.getParameter("ID"));
            int wCommandID = StringUtils.parseInt(request.getParameter("CommandID"));
            String wOrderNo = StringUtils.parseString(request.getParameter("OrderNo"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wBureauSectionID = StringUtils.parseInt(request.getParameter("BureauSectionID"));
            String wPartNo = StringUtils.parseString(request.getParameter("PartNo"));
            String wBOMNo = StringUtils.parseString(request.getParameter("BOMNo"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"));
            List<Integer> wStatusList = StringUtils.parseIntList(request.getParameter("StatusList"), ",");
            BMSEmployee wLoginUser = this.GetSession(request);

            ServiceResult<List<OMSOrder>> wServiceResult = wOMSService.OMS_QueryOMSOrderList(wLoginUser, wID,
                    wCommandID, wOrderNo, wLineID, wProductID, wBureauSectionID, wPartNo, wBOMNo, wActive, wStatusList);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 根据局段 时间段 修程 车型 车号查询订单集合
     */
    @GetMapping("/RFOrderList")
    public Object RFOrderList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            String wPartNo = StringUtils.parseString(request.getParameter("PartNo"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            BMSEmployee wLoginUser = this.GetSession(request);

            ServiceResult<List<OMSOrder>> wServiceResult = wOMSService.OMS_QueryRFOrderList(wLoginUser, wCustomerID,
                    wLineID, wProductID, wPartNo, wStartTime, wEndTime, 1);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 状态查询订单
     */
    @PostMapping("/StatusAll")
    public Object StatusAll(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            List<Integer> wStatusList = CloneTool.CloneArray(wParam.get("StatusList"), Integer.class);

            BMSEmployee wLoginUser = this.GetSession(request);

            ServiceResult<List<OMSOrder>> wServiceResult = wOMSService.OMS_QueryOMSOrderList(wLoginUser, -1, -1, "", -1,
                    -1, -1, "", "", 1, wStatusList);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_IN);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 新增或更新
     */
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            // 【订单】权限控制
            if (!wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 500903, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            if (!wParam.containsKey("data"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            OMSOrder wOMSOrder = CloneTool.Clone(wParam.get("data"), OMSOrder.class);
            if (wOMSOrder == null)
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            if (wOMSOrder.ID <= 0) {
                wOMSOrder.CreateID = wOMSOrder.ID;
                wOMSOrder.CreateTime = Calendar.getInstance();
            } else {
                wOMSOrder.EditID = wLoginUser.ID;
                wOMSOrder.EditTime = Calendar.getInstance();
            }

            ServiceResult<Long> wServiceResult = wOMSService.OMS_UpdateOMSOrder(wLoginUser, wOMSOrder);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 批量激活或禁用
     */
    @PostMapping("/Active")
    public Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            if (!wParam.containsKey("Active"))
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "参数错误!");
            int wActive = StringUtils.parseInt(wParam.get("Active"));

            if (!wParam.containsKey("data"))
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "参数错误!");

            List<OMSOrder> wList = CloneTool.CloneArray(wParam.get("data"), OMSOrder.class);
            if (wList == null || wList.size() <= 0)
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "参数错误!");

            // 【订单】权限控制
            if (wActive == 1 && !wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 500916
                            , 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }
            // 【订单】权限控制
            if (wActive == 2 && !wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 500917
                            , 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            assert wList != null;
            List<Integer> wIDList = wList.stream().map(OMSOrder::getID).collect(Collectors.toList());

            wOMSService.OMS_ActiveOMSOrderList(wLoginUser, wIDList, wActive);

            wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, null);
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 设置订单优先级
     */
    @PostMapping("/OrderPriority")
    public Object OrderPriorty(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            // 【订单优先级】权限控制
            if (!wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 500901, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            Map<String, Object> wDataParam = CloneTool.Clone(wParam.get("data"), Map.class);

            // 订单列表
            if (!wDataParam.containsKey("OrderList"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            // 优先级集合列表
            if (!wDataParam.containsKey("OMSOrderPriorityList"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            // 产线订单集合
            if (!wDataParam.containsKey("LineOrders"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            // 顾客订单集合
            if (!wDataParam.containsKey("CustomerOrders"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            // 车型ID集合
            if (!wDataParam.containsKey("ProductIDs"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            // 跨天限制时长
            if (!wDataParam.containsKey("LimitMinutes"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            // 排程性质
            if (!wDataParam.containsKey("ShiftPeriod"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            // 冗余天数
            if (!wDataParam.containsKey("RedundantDays"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            List<OMSOrder> wOrderList = CloneTool.CloneArray(wDataParam.get("OrderList"), OMSOrder.class);
            List<Integer> wPriorityIntList = CloneTool.CloneArray(wDataParam.get("OMSOrderPriorityList"),
                    Integer.class);
            List<OMSOrderPriority> wOrderPriorityList = new ArrayList<>();
            for (Integer wItem : wPriorityIntList) {
                wOrderPriorityList.add(OMSOrderPriority.getEnumType(wItem));
            }
            List<Integer> wLineOrders = CloneTool.CloneArray(wDataParam.get("LineOrders"), Integer.class);
            List<Integer> wCustomerOrders = CloneTool.CloneArray(wDataParam.get("CustomerOrders"), Integer.class);
            List<Integer> wProductIDs = CloneTool.CloneArray(wDataParam.get("ProductIDs"), Integer.class);
            int wLimitMinutes = StringUtils.parseInt(wDataParam.get("LimitMinutes"));
            int wShiftPeriod = StringUtils.parseInt(wDataParam.get("ShiftPeriod"));
            int wRedundantDays = StringUtils.parseInt(wDataParam.get("RedundantDays"));

            ServiceResult<List<OMSOrder>> wServerRst = wAPSService.APS_SetOrderPriority(wLoginUser, wOrderList,
                    wOrderPriorityList, wLineOrders, wCustomerOrders, wProductIDs);
            if (!StringUtils.isEmpty(wServerRst.getFaultCode()))
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            else
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.Result, null);

            // 保存排程条件
            ServiceResult<List<APSCondition>> wListRst = wAPSService.APS_QueryConditionList(wLoginUser, -1,
                    wShiftPeriod);
            APSCondition wAPSCondition = new APSCondition();
            if (wListRst.Result == null || wListRst.Result.size() <= 0) {
                wAPSCondition.ID = 0;
            } else {
                wAPSCondition.ID = wListRst.Result.get(0).ID;
            }
            wAPSCondition.CustomerOrders = wCustomerOrders;
            wAPSCondition.EditID = wLoginUser.ID;
            wAPSCondition.EditTime = Calendar.getInstance();
            wAPSCondition.LimitMinutes = wLimitMinutes;
            wAPSCondition.LineOrders = wLineOrders;
            wAPSCondition.OMSOrderPrioritys = wPriorityIntList;
            wAPSCondition.ProductIDs = wProductIDs;
            wAPSCondition.ShiftPeriod = wShiftPeriod;
            wAPSCondition.RedundantDays = wRedundantDays;
            wAPSService.APS_UpdateCondition(wLoginUser, wAPSCondition);
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询计划订单列表(排程用)
     */
    @GetMapping("/OrderList")
    public Object OrderList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            // 周计划、月计划
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            int wAPSShiftPeriod = StringUtils.parseInt(request.getParameter("APSShiftPeriod"));

            ServiceResult<List<OMSOrder>> wServiceResult = wOMSService.OMS_QueryHistoryOrderList(wLoginUser, wStartTime,
                    wEndTime, wAPSShiftPeriod);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询排程条件
     */
    @GetMapping("/ScheduleCondition")
    public Object ScheduleCondition(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            // 排程性质
            int wShiftPeriod = StringUtils.parseInt(request.getParameter("ShiftPeriod"));

            ServiceResult<List<APSCondition>> wServiceResult = wAPSService.APS_QueryConditionList(wLoginUser, -1,
                    wShiftPeriod);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取待竣工确认的订单列表
     */
    @GetMapping("/CompleteList")
    public Object CompleteList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<List<OMSOrder>> wServiceResult = wAPSService.APS_QueryCompleteOrderList(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 竣工确认
     */
    @PostMapping("/CompleteConfirm")
    public Object CompleteConfirm(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 【竣工确认】权限控制
            if (!wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 500905, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            // 获取参数
            int wOrderID = StringUtils.parseInt(wParam.get("OrderID"));
            List<String> wImagePathList = CloneTool.CloneArray(wParam.get("ImagePathList"), String.class);
            String wRemark = StringUtils.parseString(wParam.get("Remark"));
            List<OMSOrderLessQuestionItem> wOMSOrderLessQuestionItemList = CloneTool.CloneArray(wParam.get(
                    "OMSOrderLessQuestionItemList"), OMSOrderLessQuestionItem.class);

            ServiceResult<Integer> wServiceResult = wAPSService.APS_CompleteConfirm_V2(wLoginUser, wOrderID,
                    wImagePathList, wRemark, wOMSOrderLessQuestionItemList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取待出厂申请的订单列表
     */
    @GetMapping("/OutApplyList")
    public Object OutApplyList(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<List<OMSOrder>> wServiceResult = wAPSService.APS_QueryOutApplyOrderList(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
                this.SetResult(wResult, "OrderMap", wServiceResult.CustomResult.get("OrderMap"));
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 出厂申请
     */
    @PostMapping("/OutApply")
    public Object OutApply(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 【出厂申请】权限控制
            if (!wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 500904, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            // 获取参数
            int wOrderID = StringUtils.parseInt(wParam.get("OrderID"));
            List<String> wImagePathList = CloneTool.CloneArray(wParam.get("ImagePathList"), String.class);
            String wRemark = StringUtils.parseString(wParam.get("Remark"));

            ServiceResult<Integer> wServiceResult = wAPSService.APS_OutApply(wLoginUser, wOrderID, wImagePathList,
                    wRemark);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 出厂确认
     */
    @PostMapping("/OutConfirm")
    public Object OutConfirm(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 【出厂确认】权限控制
            if (!wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 500906, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            // 获取参数
            int wOrderID = StringUtils.parseInt(wParam.get("OrderID"));

            ServiceResult<Integer> wServiceResult = wAPSService.APS_OutConfirm(wLoginUser, wOrderID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 进厂确认单列表
     */
    @GetMapping("/InPlantList")
    public Object InPlantList(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            int wType = StringUtils.parseInt(request.getParameter("Type"));

            ServiceResult<List<OMSOrder>> wServiceResult = wAPSService.OMS_QueryInPlantList(wLoginUser, wType);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
                this.SetResult(wResult, "OrderMap", wServiceResult.CustomResult.get("OrderMap"));
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 进厂确认
     */
    @PostMapping("/InPlantConfirm")
    public Object InPlantConfirm(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 【进厂确认】权限控制
            if (!wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 500908, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            // 获取参数
            int wOrderID = StringUtils.parseInt(wParam.get("OrderID"));
            List<String> wImagePathList = CloneTool.CloneArray(wParam.get("ImagePathList"), String.class);
            String wRemark = StringUtils.parseString(wParam.get("Remark"));

            ServiceResult<Integer> wServiceResult = wAPSService.OMS_InPlantConfirm(wLoginUser, wOrderID, wImagePathList,
                    wRemark);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 批量进厂确认
     */
    @PostMapping("/BatchInPlantConfirm")
    public Object BatchInPlantConfirm(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 【进厂确认】权限控制
            if (!wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 500908, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            // 获取参数
            List<OMSOrder> wDataList = CloneTool.CloneArray(wParam.get("data"), OMSOrder.class);

            wDataList = new ArrayList<>(wDataList.stream()
                    .collect(Collectors.toMap(OMSOrder::getID, account -> account, (k1, k2) -> k2)).values());
            ServiceResult<Integer> wServiceResult = wAPSService.OMS_BatchInPlantConfirm(wLoginUser, wDataList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 修改订单的RouteID
     */
    @PostMapping("/ChangeRoute")
    public Object ChangeRoute(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 【订单工艺路线】权限控制
            if (!wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 501904, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            // 获取参数
            OMSOrder wOrder = CloneTool.Clone(wParam.get("data"), OMSOrder.class);
            Integer wRouteID = StringUtils.parseInt(wParam.get("RouteID"));
            // 参数验证
            if (wOrder == null || wOrder.ID <= 0 || wOrder.RouteID == wRouteID) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<Integer> wServiceResult = wOMSService.OMS_ChangeRoute(wLoginUser, wOrder, wRouteID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 条件查询订单
     */
    @GetMapping({"/ConditionAll"})
    public Object ConditionAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wLine = StringUtils.parseInt(request.getParameter("Line"));
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            String wWBSNo = StringUtils.parseString(request.getParameter("WBSNo"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            int wStatus = StringUtils.parseInt(request.getParameter("Status"));

            ServiceResult<List<OMSOrder>> wServiceResult = this.wOMSService.OMS_QueryConditionAll(wLoginUser,
                    wProductID, wLine, wCustomerID, wWBSNo, wStartTime, wEndTime, wStatus);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 批量保存订单
     */
    @PostMapping({"/SaveList"})
    public Object SaveTaskItemList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            List<OMSOrder> wOrderList = CloneTool.CloneArray(wParam.get("data"), OMSOrder.class);
            if (wOrderList == null || wOrderList.size() <= 0) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<Integer> wServiceResult = this.wOMSService.OMS_SaveList(wLoginUser, wOrderList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 通过ID集合查询订单集合
     */
    @PostMapping({"/IDList"})
    public Object IDList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            List<Integer> wOrderIDList = CloneTool.CloneArray(wParam.get("data"), Integer.class);
            if (wOrderIDList == null || wOrderIDList.size() <= 0) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<List<OMSOrder>> wServiceResult = this.wOMSService.OMS_QueryOrderListByIDList(wLoginUser,
                    wOrderIDList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 已收电报
     */
    @PostMapping("/Received")
    public Object Received(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 权限控制
            if (!wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 500913, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            List<OMSOrder> wOrderList = CloneTool.CloneArray(wParam.get("data"), OMSOrder.class);

            ServiceResult<Integer> wServiceResult = wOMSService.OMS_ReceivedOrder(wLoginUser, wOrderList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 订单基础数据检查
     */
    @GetMapping("/Check")
    public Object Check(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));

            ServiceResult<List<OMSCheckMsg>> wServiceResult = wOMSService.OMS_Check_V2(wLoginUser, wOrderID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 导出检验报告
     */
    @GetMapping("/Export")
    public Object Export(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));

            ServiceResult<String> wServiceResult = wOMSService.ExportCheck(wLoginUser, wOrderID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 清空台位上的车辆(内部使用)
     */
    @GetMapping("/ClearCar")
    public Object ClearCar(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));

            ServiceResult<Integer> wServiceResult = wOMSService.OMS_ClearCar(wLoginUser, wOrderID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取厂线上的所有状态为“已进厂”的车辆列表
     */
    @GetMapping("/InPlantCarList")
    public Object InPlantCarList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<List<OMSOrder>> wServiceResult = wOMSService.OMS_QueryInPlantCarList(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 剔除厂线上的所有异常车辆
     */
    @PostMapping("/ClearCarPro")
    public Object ClearCarPro(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            List<Integer> wOrderIDList = CloneTool.CloneArray(wParam.get("data"), Integer.class);

            ServiceResult<Integer> wServiceResult = wOMSService.OMS_ClearCarPro(wLoginUser, wOrderIDList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 多条件查询订单
     */
    @PostMapping("/QueryAll")
    public Object QueryAll(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            List<Integer> wCustomerList = CloneTool.CloneArray(wParam.get("CustomerList"), Integer.class);
            List<Integer> wLineList = CloneTool.CloneArray(wParam.get("LineList"), Integer.class);
            Calendar wStartTime = StringUtils.parseCalendar(wParam.get("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(wParam.get("EndTime"));
            List<Integer> wStatusList = CloneTool.CloneArray(wParam.get("StatusList"), Integer.class);
            List<Integer> wProductList = CloneTool.CloneArray(wParam.get("ProductList"), Integer.class);
            String wPartNo = StringUtils.parseString(wParam.get("PartNo"));
            List<Integer> wActiveList = CloneTool.CloneArray(wParam.get("ActiveList"), Integer.class);

            ServiceResult<List<OMSOrder>> wServiceResult = wOMSService.OMS_QueryAll(wLoginUser, wCustomerList,
                    wLineList, wStartTime, wEndTime, wStatusList, wProductList, wPartNo, wActiveList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 手动推送订单给SAP
     */
    @GetMapping("/SendToSAP")
    public Object SendToSAP(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));

            ServiceResult<String> wServiceResult = wOMSService.OMS_SendToSAP(wLoginUser, wOrderID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 根据Excel删除订单
     */
    @PostMapping("/DeleteByExcel")
    public Object DeleteByExcel(@RequestParam("file") MultipartFile[] files) {
        Object wResult = new Object();
        try {
            if (files.length == 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, "提示：没有要上传的Excel文件！");
            }

            ServiceResult<Integer> wServiceResult = new ServiceResult<>();
            ServiceResult<ExcelData> wExcelData = null;
            String wOriginalFileName = null;
            for (MultipartFile wMultipartFile : files) {
                wOriginalFileName = wMultipartFile.getOriginalFilename();

                assert wOriginalFileName != null;
                if (wOriginalFileName.contains("xlsx") || wOriginalFileName.contains("XLSX"))
                    wExcelData = ExcelReader.getInstance().readMultiSheetExcel(wMultipartFile.getInputStream(),
                            wOriginalFileName, "xlsx", 1000000);
                else if (wOriginalFileName.contains("xls") || wOriginalFileName.contains("XLS"))
                    wExcelData = ExcelReader.getInstance().readMultiSheetExcel(wMultipartFile.getInputStream(),
                            wOriginalFileName, "xls", 1000000);

                assert wExcelData != null;
                if (StringUtils.isNotEmpty(wExcelData.FaultCode)) {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR, wExcelData.FaultCode);
                    return wResult;
                }

                wServiceResult = wOMSService.OMS_DeleteByExcel(BaseDAO.SysAdmin, wExcelData.Result);

                if (!StringUtils.isEmpty(wServiceResult.FaultCode))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "删除成功!", null, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

//    /**
//     * 禁用相关计划
//     */
//    @GetMapping("/DisablePlan")
//    public Object DisablePlan(HttpServletRequest request) {
//        Object wResult = new Object();
//        try {
//            if (CheckCookieEmpty(request)) {
//                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
//                return wResult;
//            }
//
//            BMSEmployee wLoginUser = GetSession(request);
//
//            // 获取参数
//            String wOrders = StringUtils.parseString(request.getParameter("OrderIDList"));
//
//            ServiceResult<Integer> wServiceResult = wOMSService.OMS_DisablePlan(wLoginUser, wOrders);
//
//            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
//                wResult = GetResult(RetCode.SERVER_CODE_SUC, "禁用成功!", null, wServiceResult.Result);
//            } else {
//                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
//            }
//        } catch (Exception ex) {
//            logger.error(ex.toString());
//            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
//        }
//        return wResult;
//    }

    /**
     * 根据订单类型查询 已完工，未设置上级的自制件订单集合
     */
    @GetMapping("/SelfOrderList")
    public Object SelfOrderList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderType = StringUtils.parseInt(request.getParameter("OrderType"));

            ServiceResult<List<OMSOrder>> wServiceResult = wOMSService.OMS_QuerySelfOrderList(wLoginUser, wOrderType);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 订单维修
     */
    @PostMapping("/Reparing")
    public Object Reparing(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 权限控制
            if (!wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 500914, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            // 获取参数
            List<Integer> wOrderIDList = CloneTool.CloneArray(wParam.get("OrderIDList"), Integer.class);

            ServiceResult<Integer> wServiceResult = wOMSService.OMS_ReparingOrders(wLoginUser, wOrderIDList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 保存调好顺序的订单
     */
    @PostMapping("/SaveOrder")
    public Object SaveOrder(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            List<OMSOrder> wList = CloneTool.CloneArray(wParam.get("data"), OMSOrder.class);

            ServiceResult<Integer> wServiceResult = wOMSService.OMS_SaveOrder(wLoginUser, wList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 同步车号到ERP
     */
    @GetMapping("/SynchronizePartNo")
    public Object SynchronizePartNo(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));

            if (wOrderID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, "提示：参数错误，订单ID不能小于或等于0!");
            }

            ServiceResult<Integer> wServiceResult = wAPSService.OMS_SynchronizePartNo(wLoginUser, wOrderID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "同步成功!", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

//    /**
//     * 清除生产任务
//     */
//    @GetMapping("/ClearTask")
//    public Object ClearTask(HttpServletRequest request) {
//        Object wResult = new Object();
//        try {
//            if (CheckCookieEmpty(request)) {
//                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
//                return wResult;
//            }
//
//            BMSEmployee wLoginUser = GetSession(request);
//
//            // 获取参数
//            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
//
//            if (wOrderID <= 0) {
//                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
//            }
//
//            ServiceResult<Integer> wServiceResult = wAPSService.OMS_ClearAllTask(wLoginUser,
//                    wOrderID);
//
//            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
//                wResult = GetResult(RetCode.SERVER_CODE_SUC, null, "", wServiceResult.Result);
//            } else {
//                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
//            }
//        } catch (Exception ex) {
//            logger.error(ex.toString());
//            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
//        }
//        return wResult;
//    }

    /**
     * 查询待竣工确认过滤项
     */
    @GetMapping("/QueryPendingCompletionConfirmationFilterItem")
    public Object QueryPendingCompletionConfirmationFilterItem(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));

            if (wOrderID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<OMSOrderLessQuestionItem>> wServiceResult =
                    wOMSService.OMS_QueryPendingCompletionConfirmationFilterItem(wLoginUser,
                            wOrderID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 保存订单过滤问题项
     */
    @PostMapping("/SaveOrderFilterQuestionItems")
    public Object SaveOrderFilterQuestionItems(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            List<OMSOrderLessQuestionItem> wOMSOrderLessQuestionItem = CloneTool.CloneArray(wParam.get("data"),
                    OMSOrderLessQuestionItem.class);
            int wOrderID = StringUtils.parseInt(wParam.get("OrderID"));

            ServiceResult<Integer> wServiceResult = wOMSService.OMS_SaveOrderFilterQuestionItems(wLoginUser,
                    wOMSOrderLessQuestionItem, wOrderID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 将车辆绑定到厂线上
     */
    @GetMapping("/BindToLine")
    public Object BindToLine(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));

            if (wOrderID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wOMSService.OMS_BindPartNoToLine(wLoginUser, wOrderID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
